//给你一个链表的头节点 head 和一个整数 val ，
//请你删除链表中所有满足 Node.val == val 的节点，
//并返回 新的头节点 。
#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
	int data;
	struct ListNode* next;
};
struct ListNode* removeElements(struct ListNode* head, int val)
{
	struct ListNode* cur = head;
	struct ListNode* curprev = head;
	while (cur)
	{
		if (cur->data == val)
		{

			if (cur == head)
			{
				//头删
				head = cur->next;
				free(cur);
				cur = head;
			}
			else
			{
				curprev->next = cur->next;
				free(cur);
				cur = curprev->next;
			}
		}
		else
		{
			curprev = cur;
			cur = cur->next;
		}
	}
	return head;
}

void SLTNodePrintf(struct ListNode* ps)
{
	struct ListNode* cur = ps;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

int main()
{
	struct ListNode* n1 = malloc(sizeof(struct ListNode));
	struct ListNode* n2 = malloc(sizeof(struct ListNode));
	struct ListNode* n3 = malloc(sizeof(struct ListNode));
	struct ListNode* n4 = malloc(sizeof(struct ListNode));

	n1->data = 1;
	n2->data = 2;
	n3->data = 6;
	n4->data = 3;

	n1->next = n2;
	n2->next = n3;
	n3->next = n4;
	n4->next = NULL;
	SLTNodePrintf(n1);

	struct ListNode* newnode = removeElements(n1,6);
	SLTNodePrintf(n1);

	return 0;
}